﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
using CompanyBussiness;

namespace Website.customer.Control
{
    public partial class cHome : System.Web.UI.UserControl
    {
        HtmlMeta metaTag;
        NewsManagement newsMana = new NewsManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        ProductManagement proMana = new ProductManagement();
        TurnOverService overServices = new TurnOverService();
        CompanyBussiness.New news = new New();
        CompanyBussiness.Language language;
        Metadata meta;
        public int CurPage = 1;
        int TotalRow;
        int TotalPage;
        int StartRow = 0;
        int categoryIDs;
        string modulCode;

        protected void Page_Load(object sender, EventArgs e)
        {
            modulCode = Request.QueryString["Mod"];
            //loadModul();
            if (Request[CONST.QUERYSTRING_CATEGORY] != null)
                categoryIDs = int.Parse(Request[CONST.QUERYSTRING_CATEGORY].ToString());

            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }

            metaTag = new HtmlMeta();
            metaTag.Name = "keywords";
            metaTag.Content = "Trang chủ";
            Page.Header.Controls.Add(metaTag);

            metaTag = new HtmlMeta();
            metaTag.Name = "description";
            metaTag.Content = "Công ty cổ phần chế tạo điện cơ Hà Nội";
            Page.Header.Controls.Add(metaTag);

            Page.Header.Title = "Trang chủ";

            if (!Page.IsPostBack)
            {
                LoadDataListNews();
                LoadDataListTypeProduct();
            }
        }

        protected void LoadDataListNews()
        {
            dataListTopNews.DataSource = newsMana.GetTop5NewsHome(language.LanguagesID);
            dataListTopNews.DataBind();
        }

        protected void dataListTopNews_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
           e.Item.ItemType == ListItemType.AlternatingItem)
            {
                CompanyBussiness.New obj = (CompanyBussiness.New)e.Item.DataItem;

                Image imgButton = e.Item.FindControl("imgNews") as Image;

                if (obj.NewsImage == null || obj.NewsImage == string.Empty)
                {
                    imgButton.Visible = true;
                    imgButton.Style.Add("display", "none");
                }

            }
        }

        protected void LoadDataListTypeProduct()
        {
            dataListParentTypeProduct.DataSource = proMana.GetParentTypeProductNotPage(language.LanguagesID, true);
            dataListParentTypeProduct.DataBind();
        }

        protected void dataListParentTypeProduct_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
            {


                ImageButton imgButton = e.Item.FindControl("imgType") as ImageButton;

                if (imgButton.ImageUrl == null || imgButton.ImageUrl.ToString() == string.Empty)
                {
                    imgButton.Visible = true;
                }

            }
        }
    }
}